﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace ProjectManager.Masters.ModuleData
{
    #region DRDescriptions

    public class DRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private DescriptionType descType;
        private string code;

        public DRDescriptions(DescriptionType descType)
            : this(descType, false)
        { }

        public DRDescriptions(DescriptionType descType, bool updater)
            : base(updater)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = PM_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PM_Descriptions.Type);
            p2 = new SqlABParameter("@p51", PM_Descriptions.Code);

            qB.AddSelect();
            qB.AddFrom(PM_Descriptions.Name);
            qB.AddCompare(PM_Descriptions.Type, p1);
            qB.AddCompare(PM_Descriptions.Code, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
            p2.Value = code;
        }
    }

    #endregion DRDescriptions

    #region RRDescriptions

    public class RRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1;
        private DescriptionType descType;

        public RRDescriptions(DescriptionType descType)
            : base(false)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = PM_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PM_Descriptions.Type);

            qB.AddSelect();
            qB.AddFrom(PM_Descriptions.Name);
            qB.AddCompare(PM_Descriptions.Type, p1);
            qB.AddOrder(PM_Descriptions.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
        }
    }

    #endregion RRDescriptions

    #region RRProjects

    public class RRProjects : DataReaderUpdater
    {
        private SqlABParameter p1;
        private Counters descType;

        public RRProjects(Counters type)
            : base(false)
        {
            this.descType = type;
        }

        protected override void CreateConnection()
        {
            myTable = PM_Project.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PM_Project.Type);

            qB.AddSelect();
            qB.AddFrom(PM_Project.Name);
            qB.AddCompare(PM_Project.Type, p1);
            qB.AddOrder(PM_Project.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
        }
    }

    #endregion RRProjects

    #region DRProject

    public class DRProject : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private Counters type;
        private int code;

        public DRProject(Counters type, bool updater)
            : base(updater)
        {
            this.type = type;
        }

        protected override void CreateConnection()
        {
            myTable = PM_Project.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PM_Project.Type);
            p2 = new SqlABParameter("@p51", PM_Project.Code);

            qB.AddSelect();
            qB.AddFrom(PM_Project.Name);
            qB.AddCompare(PM_Project.Type, p1);
            qB.AddCompare(PM_Project.Code, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(int code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = type.Int();
            p2.Value = code;
        }
    }

    #endregion DRProject

    #region DRSession

    public class DRSession : DataReaderUpdater
    {
        private SqlABParameter p1;
        private int id;

        public DRSession(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PM_Session.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PM_Session.ID);

            qB.AddSelect();
            qB.AddFrom(PM_Session.Name);
            qB.AddCompare(PM_Session.ID, p1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(int id)
        {
            this.id = id;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = id;
        }
    }

    #endregion DRSession
}